<template>
  <div class="app-container">
      <el-form ref="departmentForm" :rules="rules" :model="departmentForm" label-position="left" label-width="70px" style="margin-left:50px;">

        <el-form-item label="标题" prop="title">
          <el-input v-model="departmentForm.title" />
        </el-form-item>
        <el-form-item label="主管" prop="responsibilityEmployeeID">
          <el-input v-model="departmentForm.responsibilityEmployeeID" />
        </el-form-item>
        <el-form-item label="业务" prop="authorization">
          <el-transfer v-model="departmentForm.authorization" :data="applicationData" :titles="['待授权', '已授权']"></el-transfer>
        </el-form-item>
        <el-form-item label="成立日期" prop="establishDate">
          <el-date-picker 
          clearable="true"
            v-model="departmentForm.establishDate"
            value-format="yyyy-MM-dd"
            type="date">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="关闭日期" prop="closeDate">
          <el-date-picker 
          clearable="true"
            v-model="departmentForm.closeDate"
            value-format="yyyy-MM-dd"
            type="date">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="departmentForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
        </el-form-item>
      </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="saveDepartment()">
        确认
      </el-button>
      <el-button v-if="departmentForm.status.value === 5" type="danger" @click="handleDelete(departmentForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import {  saveDepartment, getDepartmentDetail, removeDepartmentByID } from '@/api/mechanism/department'
import {  getApplicationSubscribe } from '@/api/program/application'


import waves from '@/directive/waves' // waves directive

import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      applicationData:[],
      departmentForm: {
        ID: undefined,
        remark: '',
        authorization:[],
        departmentID: 0,
        title: '',
        status: {
          value:1
        }
      },
      rules: {
        type: [{ required: true, department: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, department: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, department: 'title is required', trigger: 'blur' }]
      }
    }
  },
  created() {
    let departmentID = this.$route.query && this.$route.query.departmentID
    this.loadApplicationData()
    if (departmentID !== undefined) {
      departmentID = parseInt(departmentID)
      if (!isNaN(departmentID)) {
        this.listLoading = true
        getDepartmentDetail({
          departmentID:departmentID
        }).then(response => {
          if (response.status === 200) {
            this.departmentForm = response.data
            if(this.departmentForm.authorization != null){
              this.departmentForm.authorization = this.departmentForm.authorization.split(',')
            }else{
              this.departmentForm.authorization = []
            }
            console.log(this.departmentForm)
          } else {
            this.$message({
              department: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'department' })
    },
    loadApplicationData:function(){
      getApplicationSubscribe().then((response)=>{
          if(response.status == 200){
              if(response.data.total){
                response.data.list.forEach(element => {
                    this.applicationData.push({
                        key: element['ID'],
                          label: element['title']
                    })
                  
                });
              }
          }else{
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    saveDepartment() {
      let _this = this
      this.$refs['departmentForm'].validate((valid) => {
        if (valid) {
          try {
            _this.departmentForm.status = _this.departmentForm.status.value;
            if(_this.departmentForm.status !== undefined){
              _this.departmentForm.status = 1;
            }
          } catch (error) {
            _this.departmentForm.status = 1;
          }
          if(_this.departmentForm.hasOwnProperty('ID')){
            if(_this.departmentForm.ID !== undefined){
              _this.departmentForm.departmentID = _this.departmentForm.ID;
            }

          }
          saveDepartment(_this.departmentForm).then((response) => {
            console.log(response)
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.$router.push({ path: 'department' })
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
          })
        }
      })
    },
    handleDelete(row, index) {
      removeDepartmentByID(row.ID).then((response) => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.$router.push({ path: 'department' })
          } else {
            this.$message({
              department: response.msg,
              type: 'error'
            })
          }
      })
    }
  }
}
</script>
